package org.shoukaiseki.initdict.controller;

import org.shoukaiseki.common.utils.NumberUtils;
import org.shoukaiseki.common.utils.StringUtils;
import org.shoukaiseki.initdict.handle.DictTypeEnumPackageScan;
import org.shoukaiseki.initdict.logger.InitDictLoggers;
import org.shoukaiseki.kakaroto.Interface.DictTypeEnumData;
import org.shoukaiseki.kakaroto.annotation.DictTypeEnum;
import org.shoukaiseki.supersaiyan.common.constant.UserConstants;
import org.shoukaiseki.supersaiyan.common.core.domain.base.AjaxResult;
import org.shoukaiseki.supersaiyan.common.core.domain.SysDictData;
import org.shoukaiseki.supersaiyan.common.core.domain.SysDictType;
import org.shoukaiseki.supersaiyan.system.service.ISysDictDataService;
import org.shoukaiseki.supersaiyan.system.service.ISysDictTypeService;
import org.slf4j.Logger;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/init/dict")
public class InitdictController {

    public static final Logger logger = InitDictLoggers.DEFAULT_LOGGER;

    @Resource
    ISysDictTypeService sysDictTypeService;

    @Resource
    ISysDictDataService sysDictDataService;

    /**
     * 初始化字典
     * @param appended              是否追加到字典
     * @param syncDictType          同步单个字典
     * @return
     */
    @PostMapping("/addAll")
    public AjaxResult addAll(boolean appended, String syncDictType)
    {
        List<Class> dictTypeClassList = DictTypeEnumPackageScan.getDictTypeClassList();
        for (Class clazz : dictTypeClassList) {
            boolean currentAppended=appended;
            long sort=0;
            DictTypeEnum dictTypeEnum = AnnotationUtils.findAnnotation(clazz, DictTypeEnum.class);
            String dictType = dictTypeEnum.dictType();
            if(!StringUtils.isEmpty(syncDictType)&&!StringUtils.equals(dictType,syncDictType)){
               continue;
            }
            SysDictType sysDictType = new SysDictType();
            sysDictType.setDictType(dictType);
            sysDictType.setDictName(dictTypeEnum.dictName());
            sysDictType.setRemark(dictTypeEnum.remark());
            sysDictType.setCreateUserId(UserConstants.SYS_USER_ID_ADMIN);
            SysDictType dictTypeTmp = sysDictTypeService.selectDictTypeByType(dictType);

            //如果当前字典是开启自动同步,则删除字典信息后重建
            if(dictTypeTmp!=null&&NumberUtils.isOne(dictTypeTmp.getSyncFlag())){
                sysDictDataService.deleteDictDataByDictTypes(dictTypeTmp.getDictType());
                sysDictTypeService.deleteDictTypeByIds(dictTypeTmp.getDictId());
                dictTypeTmp=null;
            }
            if(dictTypeTmp ==null){
                sysDictTypeService.insertDictType(sysDictType);
                for (Object anEnum : clazz.getEnumConstants()) {
                    SysDictData dictData = new SysDictData();
                    if(!(anEnum instanceof DictTypeEnumData)){
                        return AjaxResult.error(anEnum.getClass().getSimpleName()+"需要继承DictTypeEnumData接口");

                    }
                    if(((DictTypeEnumData) anEnum).isIgnoreInsert()){
                        //忽略插入
                        continue;
                    }
                    sort++;
                    dictData.setDictValue(((DictTypeEnumData) anEnum).getValueString());
                    dictData.setDictLabel(((DictTypeEnumData) anEnum).getLabel());
//                    dictData.setDictSort(NumberUtils.convertNumberToTargetClass(((DictTypeEnumData) anEnum).getSort(),Long.class));
                    dictData.setDictSort(sort*10);
                    dictData.setDictType(dictType);
                    dictData.setCreateUserId(UserConstants.SYS_USER_ID_ADMIN);
                    dictData.setRemark(((DictTypeEnumData) anEnum).getRemark());
                    dictData.setStatus(((DictTypeEnumData) anEnum).getStatus());
                    dictData.setCssClass(((DictTypeEnumData) anEnum).getCssClass().getCssClass());
                    sysDictDataService.insertDictData(dictData);
                }
            }else{
                if(currentAppended){
                    for (Object anEnum : clazz.getEnumConstants()) {
                        SysDictData dictData = new SysDictData();
                        if(!(anEnum instanceof DictTypeEnumData)){
                            return AjaxResult.error(anEnum.getClass().getSimpleName()+"需要继承DictTypeEnumData接口");
                        }
                        if(((DictTypeEnumData) anEnum).isIgnoreInsert()){
                            //忽略插入
                            continue;
                        }
                        sort++;
                        String valueString = ((DictTypeEnumData) anEnum).getValueString();
                        logger.debug("dictType={},valueString={}",dictType, valueString);
                        String dictLabelTmp = sysDictDataService.selectDictLabel(dictType, valueString);
                        if(StringUtils.isEmpty(dictLabelTmp)){
                            dictData.setDictValue(valueString);
                            dictData.setDictLabel(((DictTypeEnumData) anEnum).getLabel());

//                            dictData.setDictSort(NumberUtils.convertNumberToTargetClass(((DictTypeEnumData) anEnum).getSort(),Long.class));
                            dictData.setDictSort(sort*10);
                            dictData.setDictType(dictType);
                            dictData.setCreateUserId(UserConstants.SYS_USER_ID_ADMIN);
                            dictData.setRemark(((DictTypeEnumData) anEnum).getRemark());
                            dictData.setStatus(((DictTypeEnumData) anEnum).getStatus());
                            dictData.setCssClass(((DictTypeEnumData) anEnum).getCssClass().getCssClass());
                            sysDictDataService.insertDictData(dictData);
                        }
                    }
                }
            }
        }
        return AjaxResult.success("成功");
    }




}
